package com.baize.filedowndemo

import android.annotation.TargetApi
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import com.baize.filedowndemo.autoupdate.UpgradeService
import com.baize.filedowndemo.utils.UpgradeHelper
import java.io.File

class NotificationActivity : AppCompatActivity() {

  val handler: Handler by lazy {
    Handler(Looper.getMainLooper())
  }

  lateinit var upgradeHelper: UpgradeHelper

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_notification)
    upgradeHelper = UpgradeHelper(this)
    lifecycle.addObserver(upgradeHelper)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      var channelId = "message"
      var channelName = "聊天消息"
      var importance = NotificationManager.IMPORTANCE_HIGH
      createNotificationChannel(channelId, channelName, importance)

      channelId = "download"
      channelName = "下载通知"
      importance = NotificationManager.IMPORTANCE_DEFAULT
      createNotificationChannel(channelId, channelName, importance)
    }
    findViewById<Button>(R.id.btn_send_message).setOnClickListener {
      val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = manager.getNotificationChannel("message")
        if (channel.importance == NotificationManager.IMPORTANCE_NONE) {
          val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
          intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
          intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.id)
          startActivity(intent)
          Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show()
        }
      }

      val notification: Notification =
        NotificationCompat.Builder(this, "message").setContentTitle("收到一条聊天消息")
          .setContentText("今天中午吃什么？").setWhen(System.currentTimeMillis())
          .setSmallIcon(R.drawable.ic_launcher_foreground)
          .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_background))
          .setNumber(2).setAutoCancel(true).build()
      manager.notify(1, notification)
    }
    findViewById<Button>(R.id.btn_send_download).setOnClickListener {
      val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
      val notification: Notification =
        NotificationCompat.Builder(this, "download").setContentTitle("下载apk中...")
          .setContentText("地铁沿线30万商铺抢购中！").setWhen(System.currentTimeMillis())
          .setSmallIcon(R.drawable.ic_launcher_foreground)
          .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_background))
          .setAutoCancel(false).build()
      manager.notify(2, notification)
    }
    findViewById<Button>(R.id.btn_download_service).setOnClickListener {
      val downloadService = Intent(this, UpgradeService::class.java)
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(downloadService)
      } else {
        startService(downloadService)
      }
    }
    findViewById<Button>(R.id.btn_bind_service).setOnClickListener {
      upgradeHelper.startDownload("dodo3.6.5.apk")
    }
    findViewById<Button>(R.id.btn_install).setOnClickListener {
      val file = File(cacheDir, "dodo3_6_5.apk")
      if (file.exists()) {
        upgradeHelper.installApk(file)
      } else {
        Toast.makeText(this, "文件不存在!", Toast.LENGTH_SHORT).show()
      }
    }
    findViewById<Button>(R.id.btn_upgrade).setOnClickListener {
      upgradeHelper.checkDeleteCacheApk()
      upgradeHelper.checkUpgradeReady("3.6.5", null,false)
    }
    handler.postDelayed({ upgradeHelper.checkUpgradeReady("3.6.5", "1.修复若干bug \n" + "2.更新若干功能",false) }, 2000)
  }

  @TargetApi(Build.VERSION_CODES.O)
  private fun createNotificationChannel(channelId: String, channelName: String, importance: Int) {
    val channel = NotificationChannel(channelId, channelName, importance)
    channel.setShowBadge(true)
    val notificationManager = getSystemService(
      NOTIFICATION_SERVICE
    ) as NotificationManager
    notificationManager.createNotificationChannel(channel)
  }

  override fun onDestroy() {
    super.onDestroy()
    Log.i("baize_", "onDestroy: ")
  }

}